<?php
namespace app\manage\controller;
use app\manage\controller\Base;

use app\model\AdminModel;
use app\model\ManageLogModel;
use app\service\CateService;
use think\facade\Db;
use support\View;
use support\Request;
use app\model\CateModel;

class SlogController extends Base
{
    public function web(Request $request)
    {
        $content='';
        $date=$request->input('date','');
        View::assign('date',$date);
        if(request()->method()=='POST'){
            $date=$request->post('date','');
            if($date){
                $file = runtime_path().DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'webman-'.$date.'.log';
                if (file_exists($file)) {
                    // 读取整个文件内容（字符串）
                    $content = file_get_contents($file);
                    // 转义 HTML 特殊字符
                    $escapedContent = htmlspecialchars($content, ENT_QUOTES, 'UTF-8');
                    $content = str_replace(["\r\n", "\n"], '<br>', $escapedContent);

                    // 可选：忽略换行符（适用于纯文本处理）
//                    $contentNoNewlines = file_get_contents($file, false, null, 0, -1, FILE_IGNORE_NEW_LINES);
//                    echo $contentNoNewlines;
                }
            }
        }
        View::assign('content', $content);
        $dateArray = [];
        $currentDate = new \DateTime();
        for ($i = 0; $i < 60; $i++) {
            $dateArray[] = $currentDate->format('Y-m-d');
            $currentDate->modify('-1 day');
        }
        View::assign('dateArray', $dateArray);
       	return view('slog/web');
    }

    public function manage(Request $request)
    {
        $info= ManageLogModel::order('id desc')->paginate(['list_rows'=>50,'query'=>request()->all()])->each(function ($item, $key) {
            $item['username']=AdminModel::where('id',$item['uid'])->value('username');
            return $item;
        });
        View::assign('info', $info);
        View::assign('count1', $info->total());
        return view('slog/manage');
    }

}
